	var filterConditions=[];
	var checkedCondition=false;
	function handleFieldArray(webField,obj){
		if(webField.indexOf('.')==-1){
				if(webField.indexOf('[]')==-1){
					return obj[webField];
				}else{
					webField=webField.substr(webField.indexOf(2,webField.length));
					var values=new Array();
					for(var i=0;i<obj[webField].length;i++){
						values.push(obj[webField][i]);
					}
					return values.join(";");
				}
			}else{
				var firstField=webField.substr(0,webField.indexOf('.'));
				var leftField=webField.substr(webField.indexOf('.')+1,webField.length);
				if(firstField.indexOf('[]')==-1){
					return handleFieldArray(leftField,obj[firstField]);
				}else{
					firstField=firstField.substr(2,firstField.length);
					var values=new Array();
					for(var i=0;i<obj[firstField].length;i++){
						var v=handleFieldArray(leftField,obj[firstField][i]);
						values.push(v);			
					}
					return values.join(";");
				}
			}
	}
	var dataGrid;
	//var webRootPath=nsl.getContextPath();
	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			url : contextPath+'/project/gridData.json',
			fit : true,
			fitColumns : true,
			singleSelect:true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40, 50 ],
			sortName : 'projectName',
			sortOrder : 'asc', 
			multiSort : true,
			striped: true,
			checkOnSelect : false,
			selectOnCheck : false,
			nowrap : false,
			frozenColumns : [ [ {
				field : 'sn',
				title : '序号',
				width : 150,
				checkbox : true
			}, {
				field : 'projectName',
				title : '业绩名称',
				width : 350,
				sortable : true,
				formatter: function(value,row,index){
					var projectId=row.id;
					var designCode=row.designCode;
					/* console.log(projectId); */
					if(typeof(value) == "undefined" ){
						value='';
					}
					//value=$.formatString('<div class="project-detail" onclick="addProjectDetailTab(\'{0}\',\'{1}\')"> {2}</div> ',projectId,designCode, value);
					return value;
				}
			} ] ],
			columns : [ [ {
				field : 'designCode',
				title : '设计编号',
				width : 75,
				sortable : true,
				hidden : false,
			}, {
				field : 'classByProfession',
				title : '项目专业',
				width : 100,
				sortable : true
			},{
				field : 'partA',
				title : '委托单位',
				width : 150,
				sortable : true,
				formatter: function(value,row,index){
					/* if(typeof(value) == "undefined" ){
						value='';
					}else{
						var pos=value.indexOf(":")+1;
						value=value.substring(pos,value.length);
						
					} */
					//value=$.formatString('<div class="project-detail" onclick="addProjectDetailTab(\'{0}\',\'{1}\')"> {2}</div> ',projectId,designCode, value);
					return value;
				}
			}, {
				field : 'investAmount',
				title : '投资金额(万元)',
				width : 100,
				sortable : true
			},{
				field : 'classByPhase',
				title : '阶段类型',
				width : 100,
				sortable : true
			},
			/* {
				field : 'classByImportance',
				title : '项目类型',
				width : 75,
				sortable : true
			}, */
			{
				field : 'contractAmount',
				title : '合同金额(万元)',
				width : 100,
				sortable : true
			},{
				field : 'year',
				title : '年份',
				width : 75,
				sortable : true,
				formatter: function(value,row,index){
					if(typeof(value) == "undefined" ){
						value='';
						return value;
					}else{
						value=value+'年';
						
					}
					return value;
				}
				
			},
			/* ,{
				field : 'beginDate',
				title : '委托日期',
				width : 75,
				sortable : true,
				formatter: function(value,row,index){
					if(typeof(value) == "undefined" ){
						value='';
						return value;
					}else{
						if(value.trim()!=''){
							value=value.substring(0,4)+'年';
							return value;
						}
					}
					return value;
				}
				
			},{
				field : 'departmentNames',
				title : '承担部门',
				width : 75,
				sortable : true
			},{
				field : 'designerNames',
				title : '设计负责人',
				width : 75,
				sortable : true
			}, */
			{
				field : 'action',
				title : '操作',
				width : 100,
				formatter : function(value, row, index) {
					var str = '';
					//详情页
					if ($.canDetail) {
					str += $.formatString('<img onclick="addProjectDetailTab(\'{0}\',\'{1}\');" src="{2}" title="详情"/>', row.id,row.designCode, contextPath+'/img/icon/extEasyUi/images/extjs_icons/book_open.png');
					}
					str += '&nbsp;&nbsp;&nbsp;&nbsp;';
					if ($.canDelete) {
						str += $.formatString('<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>', row.id, contextPath+'/img/icon/extEasyUi/images/extjs_icons/cancel.png');
					}
					return str;
				}
			} ] ],
			toolbar : '#toolbar',
			onLoadSuccess : function() {
				$('#searchForm table').show();
				parent.$.messager.progress('close');

				$(this).datagrid('tooltip');
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll').datagrid('uncheckAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			},
			onBeforeLoad : function(param){
				console.log(param);
				if(param.sort){
					var options=$(this).datagrid('getColumnOption',param.sort);
					param.webClass="project";
					console.log(param);
				}
			}
			
			
			/* onSortColumn: function (sort, order) {
				sort="sn";
				console.log(sort);
			} */
		});
		
		//datagride end
		
		
		/* console.log(dataGrid);
		console.log(dataGrid.datagrid('options')); */
		
	});

	function editPwdFun(id) {
		dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		parent.$.modalDialog({
			title : '编辑用户密码',
			width : 500,
			height : 300,
			href : '${pageContext.request.contextPath}/userController/editPwdPage?id=' + id,
			buttons : [ {
				text : '编辑',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function deleteFun(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			console.log(rows);
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
		/* 	dataGrid.datagrid('unselectAll').datagrid('uncheckAll'); */
			dataGrid.datagrid('unselectAll');
		}
		console.log("delete id="+id);
		parent.$.messager.confirm('询问', '您是否要删除当前项目？', function(b) {
			if (b) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					$.post(contextPath+'/projectController/deleteProject', {
						id : id
					}, function(result) {
						if (result.success) {
							parent.$.messager.alert('提示', result.msg, 'info');
							dataGrid.datagrid('reload');
						}
						parent.$.messager.progress('close');
					}, 'JSON');
			}
		});
	}

	function batchDeleteFun() {
		var rows = dataGrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.$.messager.confirm('确认', '您是否要删除当前选中的项目？', function(r) {
				if (r) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					var currentUserId = '${sessionInfo.id}';/*当前登录用户的ID*/
					var flag = false;
					for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].id);
							flag = true;
						}
					
					console.log(ids);
					$.getJSON(contextPath+'/projectController/batchDelete', {
						ids : ids.join(',')
					}, function(result) {
						if (result.success) {
							dataGrid.datagrid('load');
							dataGrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
						}
						parent.$.messager.alert('提示', result.msg, 'info');
						parent.$.messager.progress('close');
					});
				}
			});
		} else {
			parent.$.messager.show({
				title : '提示',
				msg : '请勾选要删除的记录！'
			});
		}
	}

	function detailFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		
	}

	function addFun() {
		parent.$.modalDialog({
			title : '添加用户',
			width : 500,
			height : 300,
			href : contextPath+'/userController/addPage',
			buttons : [ {
				text : '添加',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function batchGrantFun() {
		var rows = dataGrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				ids.push(rows[i].id);
			}
			parent.$.modalDialog({
				title : '用户授权',
				width : 500,
				height : 300,
				href : contextPath+'/userController/grantPage?ids=' + ids.join(','),
				buttons : [ {
					text : '授权',
					handler : function() {
						parent.$.modalDialog.openner_dataGrid = dataGrid;//因为授权成功之后，需要刷新这个dataGrid，所以先预定义好
						var f = parent.$.modalDialog.handler.find('#form');
						f.submit();
					}
				} ]
			});
		} else {
			parent.$.messager.show({
				title : '提示',
				msg : '请勾选要授权的记录！'
			});
		}
	}

	function grantFun(id) {
		dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		parent.$.modalDialog({
			title : '用户授权',
			width : 500,
			height : 300,
			href : contextPath+'/userController/grantPage?ids=' + id,
			buttons : [ {
				text : '授权',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为授权成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function searchFun() {
		dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
	}
	function cleanFun() {
		$('#searchForm input').val('');
		dataGrid.datagrid('load', {});
	}
	
	//增加详情显示页面
	 function addProjectDetailTab(projectId,designCode) {
		parent.addProjectDetailTab(projectId,designCode);
	} 
	
	//增加新项目tab
	 function addNewProjectTab() {
		parent.addNewProjectTab();
	} 
	
	//excle 下载
	function downloadAll (type){
		/* var myframe=document.getElementById('downloadframe');
        myframe.src="/download"; */
        var excelUrl=contextPath+"/projectController/downloadExcelAll";
        var docxUrl=contextPath+"/projectController/downloadDocxAll";
        if(type=='excel'){
        	console.log("download excel");
        	window.open(excelUrl);
        }
        if(type=='docx'){
        	console.log("download docx");
        	window.open(docxUrl);
        }
	}
			
	function downloadChecked(type){
		var excelUrl=contextPath+"/projectController/downloadExcelChecked?ids=";
        var docxUrl=contextPath+"/projectController/downloadDocxChecked?ids=";
		var rows = dataGrid.datagrid('getChecked');
		var idArray = [];
		if (rows.length > 0) {
			var flag = false;
			for ( var i = 0; i < rows.length; i++) {
					idArray.push(rows[i].id);
					flag = true;
				}
			var ids=idArray.join(",");
			if(type=='excel'){
	        	window.open(excelUrl+ids);
	        	console.log("download excel");
	        }
	        if(type=='docx'){
	        	window.open(docxUrl+ids);
	        	console.log("download docx");
	        }
		}else{
			parent.$.messager.alert('下载提示','未勾选任何项目');  
		}
	}
	
	function downloadFiltered(type){
		var excelUrl=contextPath+"/projectController/downloadExcelFiltered";
	    var docxUrl=contextPath+"/projectController/downloadDocxFiltered";
		var conditions=new Array();
		var classField=$("#project-field").val()
		var value=$("#project-field-value").val().trim();
		
		conditions=filterConditions.slice(0);
		
		if(value!=''){
			var condition=new Object();
			var valueArray=new Array();
			value="&"+value;
			valueArray.push(value);

			var className=classField.split(".")[0];
			var classField=classField.split(".")[1];
			condition.className=className;
			condition.classField=classField;
			condition.valueArray=valueArray;
			console.log(condition);
			conditions.push(condition);
		}
		var conditionsJson=JSON.stringify(conditions);
		console.log(conditionsJson);
		if(type=='excel'){
			console.log("download excel");
        	var tempForm=nsl.post(excelUrl,{"conditionsJson":conditionsJson});
        }
        if(type=='docx'){
        	console.log("download docx");
        	var tempForm=nsl.post(docxUrl,{"conditionsJson":conditionsJson});
        }
		
		nsl.removeElement(tempForm);
	}
	
	//datagrid 筛选入口函数，结合filter 和   search	
	function datagridQuery(){
		var conditions=new Array();
		var classField=$("#project-field").val()
		var value=$("#project-field-value").val().trim();
		
		console.log(value);
		console.log(filterConditions);
		
		conditions=filterConditions.slice(0);
		
		if(value!=''){
			var condition=new Object();
			var valueArray=new Array();
			value="&"+value;
			valueArray.push(value);

			var className=classField.split(".")[0];
			var classField=classField.split(".")[1];
			condition.className=className;
			condition.classField=classField;
			condition.valueArray=valueArray;
			console.log(condition);
			conditions.push(condition);
		}

		//增加仅显示筛选的filter
		if(checkedCondition){
			console.log("in checked filter");
			console.log($("#toggleCheckedFilterButton").linkbutton("options").selected);
			var idArray=filterByChecked();
			if(idArray.length>0){
				var condition=new Object();
				var className='project';
				var classField='id';
				var valueArray=idArray;
				console.log(idArray);
				condition.className=className;
				condition.classField=classField;
				condition.valueArray=valueArray;
				conditions.push(condition);
				console.log("checked condition:");
				console.log(condition);
			}
		}
		
		console.log(conditions);
		var conditionsJson=JSON.stringify(conditions);
		console.log(conditions);
		dataGrid.datagrid('load', {"conditionsJson":conditionsJson}); 
	}

	function removeSearch(){
		$("#project-field-value").val('');
		datagridQuery();
	}

	function filterByChecked(){
		var rows = dataGrid.datagrid('getChecked');
		var idArray = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
					idArray.push("="+rows[i].id);
				}
		}
		return idArray;
	}

	function toggleCheckedFilter(){
		var idArray =filterByChecked();
		if(idArray.length==0){
			alert("未勾选 任何行~")
		}else{
			checkedCondition=!checkedCondition;
			datagridQuery();
		}
	}
	
	function downloadTemplate(){
		var templateURl=contextPath+"/projectController/downloadExcelTemplate";
		nsl.post(templateURl,{});
	}
	
	function uploadExcel(){
		
		alert("Excel信息入库功能正在开发～～少安毋躁");
	}


$(document).ready(function() {

	var crumb1=new FilterCrumb("crumbId1",function(conditions){
		filterConditions=conditions;
		datagridQuery();
		/* var conditionsJson=JSON.stringify({"conditionsJson":conditions}); */
		
		/* if(queryParameter==""){
				table.ajax.url("/bid/employee/listAll.do").load();
		 }else{
			table.ajax.url("/bid/employee/queryByHql.do?condition="+encodeURI(encodeURI(queryParameter))).load();
		 }   */
	});


/* var filterRow=new FilterRow("row1",crumb1);
var filterRow=new FilterRow("row2",crumb1); */
var rowClass='project';
var rowField="departmentNames";
var rowName="承担部门";
var rowId="id_department"
var isFirstRow=true;
var items=[{
				title:'一分',
				value: "=一分",
				text:'一分'
			},
			{
				title:'二分',
				value:"=二分" ,
				text:'二分'
			},
			{
				title:'三分',
				value:"=三分" ,
				text:'三分'
			},
			{
				title:'华北',
				value:"=华北" ,
				text:'华北'
			},{
				title:'华中',
				value:"=华中" ,
				text:'华中'
			},{
				title:'=华南',
				value:"=华南" ,
				text:'华南'
			},
			{
				title:'华东',
				value:"=华东" ,
				text:'华东'
			},
			{
				title:'西南',
				value:"=西南" ,
				text:'西南'
			},
			{
				title:'建筑',
				value:"=建筑" ,
				text:'建筑'
			},
			{
				title:'软信',
				value:"=软信" ,
				text:'软信'
			},
			{
				title:'网优',
				value:"=网优" ,
				text:'网优'
			},{
				title:'生产技术部',
				value:"生产技术部" ,
				text:'生产技术部'
			}];
	var rowClass1='project';
	var rowField1="classByProfession";
	var rowName1="项目专业";
	var rowId1="id_education"
	var isFirstRow1=false;
	var items1=[
				{
					title:'3G/4G宏站（含配套电源）',
					value:"&3G/4G宏站（含配套电源）" ,
					text:'3G/4G宏站（含配套电源）'
				},
				{
					title:'铁塔',
					value:"&铁塔" ,
					text:'铁塔'
				},{
					title:'数据',
					value:"&数据" ,
					text:'数据'
				},{
					title:'室分、WLAN',
					value:"&室分、WLAN" ,
					text:'室分、WLAN'
				},{
					title:'平台',
					value:"&平台" ,
					text:'平台'
				},{
					title:'主干光缆网',
					value:"&主干光缆网" ,
					text:'主干光缆网'
				},{
					title:'基站接入配套（设备/线路）',
					value:"&基站接入配套（设备/线路）" ,
					text:'基站接入配套（设备/线路）'
				},{
					title:'传送网',
					value:"&传送网" ,
					text:'传送网'
				},{
					title:'核心网',
					value:"&核心网" ,
					text:'核心网'
				},{
					title:'交直流电源系统、节能减排',
					value:"&交直流电源系统、节能减排" ,
					text:'交直流电源系统、节能减排'
				},{
					title:'局房',
					value:"&局房" ,
					text:'局房'
				},{
					title:'宽带接入网',
					value:"&宽带接入网" ,
					text:'宽带接入网'
				},{
					title:'其它',
					value:"&其它" ,
					text:'其它'
				},
				];
	var rowClass2='project';
	var rowField2="investAmount";
	var rowName2="投资金额";
	var rowId2="id_investAmount"
	var isFirstRow2=false;
	var items2=[{
					title:'0-200万',
					value:"[0,200]" ,
					text:'0-200万'
				},{
					title:'200-500万',
					value:"[200,500]" ,
					text:'200-500万'
				},{
					title:'500-1000万',
					value:"[500,1000]" ,
					text:'500-1000万'
				},{
					title:'1000-3000万',
					value:"[1000,3000]" ,
					text:'1000-3000万'
				},{
					title:'3000-5000万',
					value:"[3000,5000]" ,
					text:'3000-5000万'
				},{
					title:'大于5000万',
					value:">5000" ,
					text:'大于5000万'
				}
				];
	var rowClass3='project';
	var rowField3="partA";
	var rowName3="委托单位";
	var rowId3="id_partA"
	var isFirstRow3=true;
	var items3=[{
					title:'河南联通',
					value:"&河南联通" ,
					text:'河南联通'
				},{
					title:'河南电信',
					value:"&河南电信" ,
					text:'河南电信'
				},{
					title:'山西移动',
					value:"&山西移动" ,
					text:'山西移动'
				},{
					title:'云南联通',
					value:"&云南联通" ,
					text:'云南联通'
				},{
					title:'湖北移动',
					value:"&湖北移动" ,
					text:'湖北移动'
				},{
					title:'湖北联通',
					value:"&湖北联通" ,
					text:'湖北联通'
				},{
					title:'湖北电信',
					value:"&湖北电信" ,
					text:'湖北电信'
				},{
					title:'西藏移动',
					value:"&西藏移动" ,
					text:'西藏移动'
				},{
					title:'西藏联通',
					value:"&西藏联通" ,
					text:'西藏联通'
				},{
					title:'西藏电信',
					value:"&西藏电信" ,
					text:'西藏电信'
				},{
					title:'贵州联通',
					value:"&贵州联通" ,
					text:'贵州联通'
				},{
					title:'江苏联通',
					value:"&江苏联通" ,
					text:'江苏联通'
				},{
					title:'其他',
					value:"&其他" ,
					text:'其他'
				}];
	
	var rowClass4='project';
	var rowField4="year";
	var rowName4="年份";
	var rowId4="id_year"
	var isFirstRow4=false;
	var items4=[{
					title:'2010年',
					value:"=2010" ,
					text:'2010年'
				},{
					title:'2011年',
					value:"=2011" ,
					text:'2011年'
				},{
					title:'2012年',
					value:"=2012" ,
					text:'2012年'
				},{
					title:'2013年',
					value:"=2013" ,
					text:'2013年'
				},{
					title:'2014年',
					value:"=2014" ,
					text:'2014年'
				},{
					title:'2015年',
					value:"=2015" ,
					text:'2015年'
				}];
	
	/* var filterTest=createFilterRow(rowId,rowClass,rowField,rowName,items,isFirstRow,crumb1,"filter_group"); */
	var filterTest4=createFilterRow(rowId3,rowClass3,rowField3,rowName3,items3,isFirstRow3,crumb1,"filter_group");
	var filterTest2=createFilterRow(rowId1,rowClass1,rowField1,rowName1,items1,isFirstRow1,crumb1,"filter_group");
	var filterTest3=createFilterRow(rowId2,rowClass2,rowField2,rowName2,items2,isFirstRow2,crumb1,"filter_group"); 
	var filterTest3=createFilterRow(rowId4,rowClass4,rowField4,rowName4,items4,isFirstRow4,crumb1,"filter_group"); 
	
	
});	
